const fs = require('fs');
const path = require('path');
const pluginName = 'Plugin02';

class Plugin02 {
	constructor(options) {
		this.options = JSON.parse(JSON.stringify(options));
		console.log('Plugin02', 'constructor', options);
	}
	apply(compiler) {
		const { options } = this;
		const defaultTemplate = './lib/Plugin02_template.html'; 
    	const lastfilename = options.filename || path.resolve(__dirname, '../dist/index2.html');

    	console.log('defaultTemplate',  defaultTemplate);
    	console.log('lastfilename',  lastfilename);
		compiler.hooks.emit.tapAsync('Plugin02', (compilation, cb) => {
			console.log('Plugin02', 'run', '构建过程开始');
			const data = fs.readFileSync(defaultTemplate);
			var html = data.toString().replace('<!-- built files will be auto injected -->', '<script src="./main.js"></script>');

            fs.writeFileSync(lastfilename, html);
            console.log('模版写入成功')
            cb()
		});
	}
}

module.exports = Plugin02;